package com.ruoyi.smart.robot;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import java.util.Scanner;

public class RobotApi {

    private static final String ROBOT_API_HOST = "http://openapi.tuling123.com/openapi/api/v2";
    private static final String apiKey = "c1f34c0b8a8a49a9a604b15ea760719e";
    private static final String userId = "10509";

    public static void main(String[] args) {
        RobotApi api = new RobotApi();
        while (true) {
            Scanner input = new Scanner(System.in);

            String word = input.next();
            String backString = api.getRobotResult(word);

            JSONObject jsonobj = JSON.parseObject(backString);
            JSONArray result = jsonobj.getJSONArray("results");
            for (int i = 0; i < result.size(); i++) {
                System.out.println(result.getJSONObject(0).getJSONObject("values").getString("text"));
            }
        }
    }

    public RobotApi() {
    }

    private String buildParams(String Word) {
        String a = "{\"reqType\":0,\"perception\": {\"inputText\": {\"text\": \"";
        String b = "\"}},\"userInfo\": {\"apiKey\": \"";
        String c = "\",\"userId\": \"";
        String d = "\"}\r\n" + "}";
        String string = a + Word + b + apiKey + c + userId + d;
        return string;
    }

    public String getRobotResult(String word) {

        return HttpPost.doPost(ROBOT_API_HOST, buildParams(word));
    }
}
